iT邦幫忙

2024 iThome 鐵人賽

DAY 15
0
Mobile Development

畢業專題拯救計畫系列 第 15

Flutter中的Plugin--url_launcher

  • 分享至 

  • xImage
  •  

url_launcher 是 Flutter 中一個常用的插件,用於打開外部應用或在瀏覽器中打開 URL。這個插件讓你可以在應用中啟動地圖、撥打電話、發送郵件或者在默認瀏覽器中打開一個網頁。

主要用途

  • 打開網頁:在默認瀏覽器中打開指定的 URL。
  • 撥打電話:使用設備的電話應用撥打電話。
  • 發送郵件:打開郵件應用並自動填充收件人、主題和內容。
  • 發送訊息:打開訊息應用程式並自動填充收件人和內容。
  • 打開地圖:使用地圖應用顯示指定的地理位置。

添加到 pubspec.yaml

要使用這個插件,需要在 pubspec.yaml 文件中添加 dependencies:

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.3.0

導入插件

在 Dart 文件中導入 url_launcher 包:

import 'package:url_launcher/url_launcher.dart';

使用 url_launcher 來打開 Google

final Uri url = Uri.parse('https://www.google.com.tw/');

if (await canLaunchUrl(url)) {
  await launchUrl(url);
} else {
  throw 'Could not launch $url';
}

點擊中間按鈕,就會跳轉到 Google 頁面
https://ithelp.ithome.com.tw/upload/images/20240901/20163322qvV6LPtmgP.png
https://ithelp.ithome.com.tw/upload/images/20240901/20163322iJ6h3cDWDv.png
我們明天見~


上一篇
Flutter中的Third-Party Libraries--Fluttertoast
下一篇
Flutter中的Plugin--path_provider
系列文
畢業專題拯救計畫22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言